import React from 'react';
import address from '../address';
import { Modal, Button, Space } from 'antd';
import { ExclamationCircleOutlined } from '@ant-design/icons';

let token = '' || localStorage.getItem('token');
const { confirm } = Modal;
//删除文章
export const delArticle = (t)=> {
    console.log(t);
    confirm({
        title: '确认删除这篇文章？',
        icon: <ExclamationCircleOutlined />,
        content: '删除之后将永久无法显示',
        okText:'删除',
        cancelText:'取消',
        onOk() {
            fetch(address+'/admin/deletePassage', {
                method : 'DELETE',
                mode : 'cors',
                headers: {
                    'authorization':token
                },
                body : t
            }).then(res => {
                if (res.ok) {
                    console.log('ok');
                    //只有请求成功才会返回数据
                    return res.text()
                } else {
                    throw new Error(`status: ${res.status}`)
                }
                }, err => {
    
            }).then(d => {
                if (d === undefined || d === '') {
                    return
                }
                setInterval(()=>{
                    window.history.go(0);
                },500);
            })
            .catch(err => {
                console.log(err)
            })
        },
        onCancel() {
          console.log('Cancel');
        },
      });
}

//删除App
export const delApp = (t)=> {
    console.log(t);
    let delApp = window.confirm('确认删除该App？');
    if(delApp){
        fetch('http://211.159.166.29:1234/admin/deleteApp', {
            method : 'DELETE',
            mode : 'cors',
            headers: {
                'authorization':token
            },
            body : t
        }).then(res => {
            if (res.ok) {
                //只有请求成功才会返回数据
                return res.text()
            } else {
                throw new Error(`status: ${res.status}`)
            }
            }, err => {

        }).then(d => {
            if (d === undefined || d === '') {
                return
            }
            setInterval(()=>{
                window.history.go(0);
            },500);
        })
        .catch(err => {
            console.log(err)
        })
    }else{
        console.log('已取消删除。');
    }
}

//删除功能
export const delFun = (t)=> {
    let appname = t.split('_')[0];
    let ename = t.split('_')[1];
    console.log(appname,ename);
    let delFun = window.confirm('确认删除该功能？');
    if(delFun){
        let obj = {
            appname,ename
        }
        fetch('http://211.159.166.29:1234/admin/deleteAppFunc', {
            method : 'DELETE',
            mode : 'cors',
            headers: {
                'authorization':token
            },
            body : JSON.stringify(obj)
        }).then(res => {
            if (res.ok) {
                //只有请求成功才会返回数据
                return res.text()
            } else {
                throw new Error(`status: ${res.status}`)
            }
            }, err => {

        }).then(d => {
            if (d === undefined || d === '') {
                return
            }
            setInterval(()=>{
                window.history.go(0);
            },500);
        })
        .catch(err => {
            console.log(err)
        })
    }else{
        console.log('已取消删除。');
    }
}
//删除步骤
export const delStep = (t)=> {
    console.log(t);
    console.log(t.split('/'));
    let appname = t.split('/')[5];
    let ename = t.split('/')[6];
    let imgname = t.split('/')[7];
    let tablename = appname+'_'+ename;
    let delStep = window.confirm('确认删除该步骤？');
    if(delStep){
        let obj = {
            appname,imgname,tablename,ename
        }
        console.log(obj);
        fetch('http://211.159.166.29:1234/admin/deleteAppStep', {
            method : 'DELETE',
            mode : 'cors',
            headers: {
                'authorization':token
            },
            body : JSON.stringify(obj)
        }).then(res => {
            if (res.ok) {
                //只有请求成功才会返回数据
                return res.text()
            } else {
                throw new Error(`status: ${res.status}`)
            }
            }, err => {

        }).then(d => {
            if (d === undefined || d === '') {
                return
            }
            setInterval(()=>{
                window.history.go(0);
            },500);
        })
        .catch(err => {
            console.log(err)
        })
    }else{
        console.log('已取消删除。');
    }
}

